// pages/personal_details/index.js
import {
  HTTP
} from '../../../utils/http.js'
let Myhttp = new HTTP()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    img: '',
    name: '',
    login: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },
  login:function(e){
    wx.getSetting({
      success: (response) => {
        //console.log(response)
        if (!response.authSetting['scope.userInfo']) {
          Myhttp.jump({
            url: '/pages/authorize/index'
          })
          // wx.navigateTo({
          //   url: '/pages/authorize/index'
          // })
        } else {
          wx.getUserInfo({
            success: (res) => {
              this.setData({
                img: res.userInfo.avatarUrl,
                name: res.userInfo.nickName,
                login: true
              })
            },
            complete: (res) => {
              Myhttp.request({
                url: 'questionnaire/index/saveUserInfo',
                method: 'POST',
                data: {
                  nickName: res.userInfo.nickName,
                  avatarUrl: res.userInfo.avatarUrl,
                  gender: res.userInfo.gender,
                  city: res.userInfo.city,
                  province: res.userInfo.province,
                  language: res.userInfo.language
                },
                success: (response) => {

                }
              })
            }
          })
        }
      }
    })
  },
  MyIncome: function() {
    Myhttp.jump({
      url: '/pages/personal/MyIncome/index',
      success: (res) => {
      }
    })
    
  },
  partake: function() {
    Myhttp.jump({
      url: '/pages/personal/MyAnswer/index',
      success: (res) => {
      }
    })

  },
  publish: function() {
    Myhttp.jump({
      url: '/pages/PublishQuestionnaire/PublishProblem/index',
      success: (res) => {
      }
    })
  },
  Mypublish: function() {
    Myhttp.jump({
      url: '/pages/personal/MyPublish/index',
      success: (res) => {
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */

  onShow: function() {
    wx.getSetting({
      success: (response) => {
        //console.log(response)
        if (response.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: (res) => {
              console.log(res)
              this.setData({
                img: res.userInfo.avatarUrl,
                name: res.userInfo.nickName,
                login: true
              })
            },
            complete: (res) => {
              Myhttp.request({
                url: 'questionnaire/index/saveUserInfo',
                method: 'POST',
                data: {
                  nickName: res.userInfo.nickName,
                  avatarUrl: res.userInfo.avatarUrl,
                  gender: res.userInfo.gender,
                  city: res.userInfo.city,
                  province: res.userInfo.province,
                  language: res.userInfo.language
                },
                success: (response) => {

                }
              })
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      path: '/pages/personal/personal_details/index'
    }
  }
})